var util = require("../../../utils/util.js")
const api = require("../../../api/index.js")
let app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    images: {},
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    phone:'18110045265',
    courseData: [], // 课程详情数据
    userPhone: "",
    imgSrc:"",
    play:false,
    playbtton:true,
  },
  imageLoad: function (e) {
    var $width = e.detail.width,    //获取图片真实宽度
      $height = e.detail.height,
      ratio = $width / $height;    //图片的真实宽高比例
    var viewWidth = 718,           //设置图片显示宽度，左右留有16rpx边距
      viewHeight = 718 / ratio;    //计算的高度值
    var image = this.data.images;
    //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
    image[e.target.dataset.index] = {
      width: viewWidth,
      height: viewHeight
    }
    this.setData({
      images: image
    })
  },
  site: function () {
    let lat = Number(this.data.courseData.branch.branch_veidoo);
    let lon = Number(this.data.courseData.branch.branch_long);
    let titleName = this.data.courseData.branch.branch_name;
    let address = this.data.courseData.branch.branch_address
    App.zhuge.track('课程详情-打开地图导航', {
      '地图地址': titleName
    })
     util.siet(lat, lon, titleName, address);
  },
  phone:function(event){
    var phone;
    var data = event.currentTarget.dataset.phone;
    App.zhuge.track('课程详情-打开电话', {
      '电话': titleName
    })
    util.phone(data,this.data.phone);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.setData({
      imgSrc: api.imgPathUrl
    })
    this.getCourseData(options.id)
    
    this.setData({
      userPhone: wx.getStorageSync('token').user_mobile
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.onLoad();
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },
  getCourseData: function (courseId){
    let data = {
      longitude: wx.getStorageSync('key').longitude,
      latitude: wx.getStorageSync('key').latitude,
      course_id: courseId
    } 
    console.log(getCurrentPages())
    wx.showLoading({
      title: '加载中',
    })
    api.courseDetails(data, (res) => {
      let thit = this;
      console.log(res.data);
      if (res.data.code == 200){
          setTimeout(function(){
            thit.setData({
              play:true
            })
            wx.hideLoading()
        },500)
      }
      App.zhuge.track('打开课程详情页面', {
        '课程详情名称': this.data.courseData.course_name
      })
      this.setData({
        "courseData": res.data.result
      })
      
      // 动态显示页头标题。
      wx.setNavigationBarTitle({ title: this.data.courseData.course.course_name})
    })
  },
  phoneCall: function (e) { // 底部打电话

    wx.makePhoneCall({
      phoneNumber: this.data.courseData.branch.branch_tel //仅为示例，并非真实的电话号码
    })
  },
  getPhoneNumber: function (e){
      
    let k_id = this.data.courseData.course.course_id; // 当前课程id
    let v_id = this.data.courseData.branch.branch_id; // 当前场馆id
    if (this.data.playbtton == true){
      if (this.data.play == true) {
        if (e.detail.encryptedData) { // 如果用户没有授权手机号 调用Api获取手机号
          if (wx.getStorageSync('session_key').session_key == '') {
            app.Logins();
          }
          let invite_id = wx.getStorageSync('invite_id');
          let data = {
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            sessionKey: wx.getStorageSync('session_key').session_key,
            invite_id: invite_id
          }
          this.setData({
            playbtton:false
          })
          api.getPhoneNumber(data, (res) => {
            console.log(res, data);
            wx.setStorageSync('token', {
              "token": res.data.result.token,
              "user_mobile": res.data.result.user_mobile
            });
            console.log(res)
            let phone = res.data.result.user_mobile;
            let token = wx.getStorageSync('token');
            token.user_mobile = phone;
            this.setData({
              userPhone: phone
            })
            
            // wx.setStorageSync("token", token)
            // 用户同意获取手机号
            this.setData({
              playbtton: true
            })
            App.zhuge.track('获取手机号授权', {
              '用户手机': phone
            })
            wx.navigateTo({
              url: `/pages/my/play/play?k_id=${k_id}&v_id=${v_id}&mobile=${phone}?`,
            })
          })

        } else {
          // 用户不同意获取手机号
          App.zhuge.track('拒绝获取手机号授权', {})
          this.setData({
            playbtton: true
          })
          wx.showToast({
            title: '亲，下单是需要手机号的哦',
            icon: 'none',
            duration: 2000
          })

        }
      }
    }
    

    
  },
  goPlay: function(){
    let k_id = this.data.courseData.course.course_id; // 当前课程id
    let v_id = this.data.courseData.branch.branch_id; // 当前场馆id
    let userPhone = this.data.userPhone;
    App.zhuge.track('课程详情-点击立即抢购', {})
    wx.navigateTo({
      url: `/pages/my/play/play?k_id=${k_id}&v_id=${v_id}&mobile=${userPhone}`,
    })
  }
})